<?php

session_start();

// include settings and functions
include "../conn/database.php";
include "functions.php";
include 'UUID.php';

// if the user submit the form
if(isset($_POST['submit'])) {
	// initialize errors values
	$errors=0;
	$errors_text = '';
	// cleanning the post data from hacking attempt
	$post_data = clean($_POST);
	
	// empty post data verification
	if(empty($post_data['email']) || empty($post_data['password']) || empty($post_data['confirmPassword']) 
	|| empty($post_data['firstName']) || empty($post_data['lastName']) || empty($post_data['gender'])) {
		$errors_text .= "Des champs sont vides<br />";
		$errors++;
	}
	
	// password verification
	if($post_data['password'] != $post_data['confirmPassword'] 
			|| !check_password($post_data['password'])) {
		$errors_text .= "Les mots de passe ne correspondent pas<br />";
		$errors++;
	}
	
	// email verification
	if(!check_email($post_data['email']) ) {
		$errors_text .= "L'email n'est pas correct<br />";
		$errors++;
	}
	
	// gender verification (is 'MALE' or 'FEMALE')
	if(!check_gender($post_data['gender'])) {
		$errors_text .= "La civilité n'est pas valide<br />";
		$errors++;
	}
	// check if user exists
	if(!check_userexist($post_data['email'])) {
		$errors_text .= "User already exists.<br />";
		$errors++;
	}
	
	// if no errors we proceed
	if($errors == 0) {
		// data correction
		$firstName = ucfirst(strtolower($post_data["firstName"]));
		$lastName = strtoupper($post_data["lastName"]);
		$email = strtolower(filter($post_data["email"]));
		
		// generating the validation key for the email
		$key = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bccc', generate_guid(6));
		
		// generating the user ID
		$guid = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', generate_guid(6));
		$password = MD5($post_data['password']);
		
		// generating the wedding ID
		$wid = UUID::v5('1546058f-5a25-4334-85ae-e68f2a44bbaf', generate_guid(6));
		
		$insert_sql = "
			INSERT INTO `T_PERSON`
				(`PERSON_SID`, `FIRST_NAME`, `SURNAME`,`BIRTHDATE`, `EMAIL_ADDRESS`, `PASSWORD`, `GENDER`, `WANT_NEWS`)
			VALUES
				('".$guid."','".$firstName."','".$lastName."','1970-01-01','".$email."','"
				.  $password ."','".$post_data['gender']."','1')"
		;
		$insert_sql2 = "
			INSERT INTO `R_WEDDINGS_INCHARGE`
				(`PERSON_SID`, `WEDDING_ID`, `role`, `IS_ACTIVATED`, `ACTIVATE_KEY`)
			VALUES
			('".$guid."','".$wid."','1','0','".$key."')"
		;
		$insert_sql3 = "
			INSERT INTO `T_WEDDINGS`
				(`WEDDING_ID`, `WEDDING_DATE`, `IS_ACTIVE`, `FIRST_MEETING`, `LEFT_LIKES`,
				`LEFT_DISLIKES`, `RIGHT_LIKES`, `RIGHT_DISLIKES`, `INTERVIEW_ID_BOTH`,
				`INTERVIEW_ID_LEFT`, `INTERVIEW_ID_RIGHT`, `PROPOSE_DATE`, `PHOTO_ID`,
				`HAS_LIVE_OPTION`, `HAS_DATASTORAGE_OPTION`)
			VALUES
				('".$wid."','','1','','',
				'','','','',
				'','','','',
				'0','0')"
		;
		
		if(mysql_query($insert_sql) && mysql_query($insert_sql2) && mysql_query($insert_sql3)) {
			if(!smtp_mail_register($email, "Welcome!", $key, "verification@promessedunevie.com", "Promesse d'une Vie")) {
				$errors_text .= mysql_error()."<br />";
				$errors++;
			}
		} else {
			$errors_text .= mysql_error()."<br />";
			$errors++;
		}
	}
	
	if($errors == 0) {
		$arr['success'] = "1"; 
        $arr['msg'] = 'Register succeed. Please check your email.';
	} else {
		$arr['success'] = "0"; 
        $arr['msg'] = $errors_text;
	}
	echo json_encode($arr);
}
?>